--This Library allowss CoDE to override the Gcombat, CDS and PewPew damage/repair functions with its own

CDamage_ExtraEntInits = {}

function CDamage_PerformOverride()
	MsgN("Contraption Damage Engine: Commencing Override Preload")
	--Store original Gcombat functions elsewhere
	if (COMBATDAMAGEENGINE) then
		CDE_Gcombat_Default_Direct = gcombat.devhit
		CDE_Gcombat_Default_Area = gcombat.hcgexplode
		table.insert(CDamage_ExtraEntInits, gcombat.registerent)
		MsgN("Contraption Damage Engine: GCombat Detected")
	else
		MsgN("Contraption Damage Engine: GCombat NOT Detected")
		gcombat = {}
	end
	
	--Store original CDS functions elsewhere
	if (CombatDamageSystem) then
		CDE_CDS_Default_DamageEnt = cds_damageent
		CDE_CDS_Default_DamagePos = cds_damagepos
		CDE_CDS_Default_Explosion = cds_explosion
		MsgN("Contraption Damage Engine: CDS Detected")
	else
		MsgN("Contraption Damage Engine: CDS NOT Detected")
	end
	
	--Store original PewPew functions elsewhere
	if (pewpew and pewpew.Installed) then
		CDE_PP_Default_Direct = pewpew.DealDamageBase
		CDE_PP_Default_Area = pewpew.BlastDamage
		CDE_PP_Default_RepairCore = pewpew.RepairCoreHealth
		CDE_PP_Default_Repair = pewpew.RepairHealth
		CDE_PP_Default_GetHealth = pewpew.GetHealth
		CDE_PP_Default_GetMaxHealth = pewpew.GetMaxHealth
		table.insert(CDamage_ExtraEntInits, function(ent) pewpew:SetHealth( ent ) end)
		MsgN("Contraption Damage Engine: PewPew Detected")
	else
		MsgN("Contraption Damage Engine: PewPew Not Detected")
		pewpew ={}
	end
	
	
	MsgN("Contraption Damage Engine: Commencing Function Override")
	
	
	--Override Gcombat functions with our new handlers
	gcombat.devhit = CDamage_Handler_GCombat_Direct
	gcombat.hcgexplode = CDamage_Handler_GCombat_Area
	cbt_dealdevhit = CDamage_Handler_GCombat_Direct
	cbt_hcgexplode = CDamage_Handler_GCombat_Area
	
	--Override CDS functions with our new handlers
	cds_damageent = CDamage_Handler_CDS_Direct
	cds_damagepos = CDamage_Handler_CDS_Position
	cds_explosion = CDamage_Handler_CDS_Explosion
	
	--Override PewPew functions with our new handlers
	pewpew.DealDamageBase = CDamage_Handler_PewPew_Direct
	pewpew.BlastDamage = CDamage_Handler_PewPew_Explosion
	pewpew.RepairCoreHealth = CDamage_Handler_PewPew_Repair_Core
	pewpew.RepairHealth = CDamage_Handler_PewPew_Repair_Normal
	pewpew.GetHealth = CDamage_Handler_PewPew_GetHealth
	pewpew.GetMaxHealth = CDamage_Handler_PewPew_GetMaxHealth
	
	MsgN("Contraption Damage Engine: Function Override Completed")
end

hook.Add("InitPostEntity", "CDamage_Override", CDamage_PerformOverride)

function CDamage_EntityInit_Extras(entity)
	for _,func in pairs(CDamage_ExtraEntInits) do
		func(entity)
	end
end

function CDamage_Handler_GCombat_Direct(entity, damage, pierce)
	if (util.tobool(GetConVarNumber("sv_CDamage_overrideGcombat"))) or (!COMBATDAMAGEENGINE) then
		CDamage_damageent(entity, damage, nil)
		return 0
	else
		CDE_Gcombat_Default_Direct(entity, damage, pierce)
	end
end

function CDamage_Handler_GCombat_Area(position, radius, damage, pierce)
	if (util.tobool(GetConVarNumber("sv_CDamage_overrideGcombat"))) or (!COMBATDAMAGEENGINE) then
		CDamage_blastdamage(position, radius, damage, nil)
		return 0
	else
		CDE_Gcombat_Default_Area(position, radius, damage, pierce)
	end
end

function CDamage_Handler_CDS_Direct(ent, damage, pierce, inflictor, heat, gcombat)
	if (util.tobool(GetConVarNumber("sv_CDamage_overrideCDS"))) or (!CombatDamageSystem) then
		CDamage_damageent(ent, damage, inflictor)
	else
		CDE_CDS_Default_DamageEnt(ent, damage, pierce, inflictor, heat, gcombat)
	end
end

function CDamage_Handler_CDS_Position(pos, damage, pierce, radius, inflictor)
	if (util.tobool(GetConVarNumber("sv_CDamage_overrideCDS"))) or (!CombatDamageSystem) then
		CDamage_blastdamage(pos, radius, damage, inflictor)
	else
		CDE_CDS_Default_DamagePos(pos, damage, pierce, radius, inflictor)
	end
end

function CDamage_Handler_CDS_Explosion(pos, radius, damage, pierce, defeffect, inflictor)
	if (util.tobool(GetConVarNumber("sv_CDamage_overrideCDS"))) or (!CombatDamageSystem) then
		CDamage_blastdamage(pos, radius, damage, inflictor)
	else
		CDE_CDS_Default_Explosion(pos, radius, damage, pierce, defeffect, inflictor)
	end
end

function CDamage_Handler_PewPew_Explosion( This, Position, Radius, Damage, RangeDamageMul, IgnoreEnt, DamageDealer )
	if (util.tobool(GetConVarNumber("sv_CDamage_overridePewPew"))) or (!pewpew) then
		CDamage_blastdamage(Position, Radius, Damage, DamageDealer)
	else
		CDE_PP_Default_Area( This, Position, Radius, Damage, RangeDamageMul, IgnoreEnt, DamageDealer )
	end
end

function CDamage_Handler_PewPew_Direct( This, TargetEntity, Damage, DamageDealer )
	if (util.tobool(GetConVarNumber("sv_CDamage_overridePewPew"))) or (!pewpew) then
		CDamage_damageent(TargetEntity, Damage, DamageDealer)
	else
		CDE_PP_Default_Direct( This, TargetEntity, Damage, DamageDealer )
	end
end

function CDamage_Handler_PewPew_Repair_Core( This, ent, amount)
	if (util.tobool(GetConVarNumber("sv_CDamage_overridePewPew"))) or (!pewpew) then
		CDamage_repairent(ent, amount)
	else
		CDE_PP_Default_RepairCore( This, ent, amount)
	end
end

function CDamage_Handler_PewPew_Repair_Normal( This, ent, amount)
	if (util.tobool(GetConVarNumber("sv_CDamage_overridePewPew"))) or (!pewpew) then
		CDamage_repairent(ent, amount)
	else
		CDE_PP_Default_Repair( This, ent, amount)
	end
end

function CDamage_Handler_PewPew_GetHealth( This, ent )
	if (util.tobool(GetConVarNumber("sv_CDamage_overridePewPew"))) or (!pewpew) then
		if (util.tobool(GetConVarNumber("sv_CDamage_contraptiondamage"))) then
			return CDamage_findcontraptionhealth(CDE_FindContraption(ent))
		else
			return CDamage_findcontraptionhealth({ent})
		end
	else
		return CDE_PP_Default_GetHealth( This, ent)
	end
end

function CDamage_Handler_PewPew_GetMaxHealth( This, ent )
	if (util.tobool(GetConVarNumber("sv_CDamage_overridePewPew"))) or (!pewpew) then
		if (util.tobool(GetConVarNumber("sv_CDamage_contraptiondamage"))) then
			return CDamage_findcontraptionmaxhealth(CDE_FindContraption(ent))
		else
			return CDamage_findcontraptionmaxhealth({ent})
		end
	else
		return CDE_PP_Default_GetMaxHealth( This, ent )
	end
end